package com.lsmo.android.carfans;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;

import com.lsmo.android.carfans.app.view.TabsView;
import com.lsmo.android.carfans.view.car.SelectCarView;
import com.lsmo.android.carfans.view.car.SelectPriceView;
import com.lsmo.android.carfans.view.favorites.FavoritesDistributorView;
import com.lsmo.android.carfans.view.favorites.FavoritesModelView;
import com.lsmo.android.carfans.view.forum.ForumHotView;
import com.lsmo.android.common.base.BaseActivity;
import com.lsmo.android.common.base.BaseView;

public class MainActivity extends BaseActivity {
    /** Called when the activity is first created. */
  
	public static final String TAG = MainActivity.class.getSimpleName();
 
	
	private View mNewsIndexView ;     //新闻首页
	private View mCarIndexView;       //车型首页
	private View mForumIndexView;     //论坛首页
	private View mFavoritesIndexView; //收藏首页
	
	private LayoutInflater mInflater ;
	private String[] mTabsName = new String[]{
			"新闻",
			"汽车",
			"论坛",
			"收藏",
	};
	
 	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mInflater = this.getLayoutInflater();
		
		mNewsIndexView = initNews();
		mCarIndexView = initCar();
		mForumIndexView =  initForum();
		mFavoritesIndexView = initFavorites();	
 
		this.setContentView(new TabsView(this,
				TabsView.TABS_STYLE_TEXT_BOTTOM,
				mTabsName,
				new View[]{
				mNewsIndexView,
				mCarIndexView,
				mForumIndexView,
				mFavoritesIndexView
		}));

	}

   /***
    * 初始化新闻
    * @return
    */
 	private View initNews(){
 		return new TabsView(this,
 				TabsView.TABS_STYLE_TEXT_TOP,
 		new String[]{"最新","评测"},	
		new View[]{new ForumHotView(this),
				new ForumHotView(this)
		});
 	}
    /***
     * 初始化车型
     * @return
     */	
 	private View initCar(){
 		return new TabsView(this,
 				TabsView.TABS_STYLE_TEXT_TOP,
 		new String[]{"按品牌","按价格"},	
		new View[]{new SelectCarView(this),
				new SelectPriceView(this)
		});
 	}
 	
    /***
     * 初始化论坛
     * @return
     */	
 	private View initForum(){
 		
 		return new TabsView(this,
 				TabsView.TABS_STYLE_TEXT_TOP,
 		new String[]{
				"热帖","车型论坛","品牌论坛"
		},	
		new View[]{
				new ForumHotView(this),
				new BaseView(this),
				new BaseView(this)
		});
 	}
    /***
     * 初始化收藏
     * @return
     */	
 	private View initFavorites(){
 		return new TabsView(this,
 				TabsView.TABS_STYLE_TEXT_TOP,
 		new String[]{"车型","经销商"},	
		new View[]{new FavoritesModelView(this),
				new FavoritesDistributorView(this)
		});
 	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		android.os.Process.killProcess(android.os.Process.myPid());
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 如果是返回键,直接返回到桌面
		if( keyCode == KeyEvent.KEYCODE_BACK ){
			onBack();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}
	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		return super.dispatchKeyEvent(event);
	}
	
	public void onBack(){
	  AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
		.setTitle("提示").setMessage("确定要退出吗?").setPositiveButton("确定",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog,
							int which) {
						MainActivity.this.finish();
					}
				}).setNegativeButton("取消",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog,int which) {
						dialog.cancel();
					}
				}).create();

		alertDialog.show();
	}
 	
 
}